package com.cskaoyan.service.system;

import com.cskaoyan.bean.MallAdmin;
import com.cskaoyan.bean.MallAdminExample;
import com.cskaoyan.bean.MallRole;
import com.cskaoyan.mapper.MallAdminMapper;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.bo.profile.PasswordBo;
import com.cskaoyan.model.vo.PageListVo;
import com.cskaoyan.model.vo.system.SimpleMallAdmin;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: project-market
 * @description:
 * @author: Weiwei Liu
 * @create: 2021-11-29 21:11
 **/
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    MallAdminMapper mapper;

    /**
     * 查询所有管理员的信息
     *
     * @param pageBo
     * @param username
     * @return
     */
    @Override
    public PageListVo list(PageBo pageBo, String username) {

        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());

        MallAdminExample example = new MallAdminExample();

        if (username == null) username = "";
        example.createCriteria().andDeletedEqualTo(false).andUsernameLike("%" + username + "%");

        // 设置分页信息
        example.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());


        List<SimpleMallAdmin> list = mapper.selectSimpleByExample(example);

        //通过分页信息拿到分页数据
        //PageInfo的有参构造方法，放入的***查询结果***
        PageInfo info = new PageInfo(list);

        PageListVo vo = new PageListVo();

        vo.setTotal(info.getTotal());
        vo.setLimit(pageBo.getLimit());
        vo.setPage(info.getPageSize());
        vo.setPages(info.getPages());
        vo.setList(info.getList());

        return vo;

    }

    @Override
    public boolean update(MallAdmin admin) {

        Integer i = mapper.updateAdminById(admin);

        return i == 1;

    }

    /**
     * 删除管理员，把deleted更新为1
     *
     * @param admin
     * @return
     */

    @Override
    public boolean delete(MallAdmin admin) {


        admin.setDeleted(true);

        Integer i = mapper.updateAdminById(admin);

        return i == 1;

    }


    @Override
    public MallAdmin create(SimpleMallAdmin simple) {

        // 先查一下管理员是否存在
        Integer repeat = mapper.selectByUsername(simple.getUsername());
        if (repeat != 0) return null;


        // 不重复，可以增加管理员了
        MallAdmin admin = new MallAdmin();
        admin.setUsername(simple.getUsername());
        admin.setPassword(simple.getPassword());
        admin.setAvatar(simple.getAvatar());
        admin.setRoleIds(simple.getRoleIds());

        Integer i = mapper.insertSimpleAdmin(admin);

        if (i == 1) return admin;

        return null;


    }

    /**
     * 用户登录验证
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public Integer selectAdmin(String username, String password) {

        // 账号密码不合法
        if (username == null || password == null) return 10000;

        MallAdminExample example = new MallAdminExample();
        example.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);

        List<MallAdmin> mallAdmins = mapper.selectByExample(example);

        // 用户账号或者密码不对
        if (mallAdmins == null || mallAdmins.size() == 0) return 10000;

        // 用户存在
        return 0;

    }

    @Override
    public Integer password(PasswordBo bo, String username) {

        Integer i = mapper.updatePasswordByUsername(bo, username);

        if (i == 1) return 0;

        else return 10000;


    }

    /**
     * 查询roleId在admin中存在
     *
     * @param role
     * @return
     */
    @Override
    public boolean selectRoleExistAdmin(MallRole role) {


        MallAdminExample example = new MallAdminExample();

        List<MallAdmin> mallAdmins = mapper.selectByExample(example);

        for (MallAdmin mallAdmin : mallAdmins) {
            Integer[] roleIds = mallAdmin.getRoleIds();
            for (Integer roleId : roleIds) {
                if (roleId.equals(role.getId())) return true;
            }
        }

        return false;
    }
}
